package com.atguigu.crowd.mvc.handler;

import com.atguigu.crowd.service.api.AdminService;
import com.atguigu.crowd.util.CrowdUtil;
import com.atguigu.crowd.util.ResultEntity;
import entity.Admin;
import entity.Student;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class TestHandler {
    private Logger logger=LoggerFactory.getLogger(TestHandler.class);
    @Autowired
    private AdminService adminService;
    @RequestMapping("test/ssm.html")
    public String testSsm(ModelMap modelMap, HttpServletRequest request) {
        List<Admin> adminList = adminService.getAll();
        modelMap.addAttribute("adminlist", adminList);
//        测试计算异常
//        System.out.println(1/0);
        logger.info(String.valueOf(CrowdUtil.judgeRequestType(request)));
//        测试空指针异常
//        String A = null;
//        System.out.println(A.length());
        return "target";
    }
    @ResponseBody
    @RequestMapping("send/array/one.html")
    public String testSsm2(@RequestParam("array[]") List<Integer> array,HttpServletRequest request) {
        for(Integer number:array){
            System.out.println("Number="+number);

        }
        logger.info(String.valueOf(CrowdUtil.judgeRequestType(request)));
//        System.out.println(1/0);
//        String A = null;
//        System.out.println(A.length());
//        服务器返回success
        return "success";
    }
    //通过@RequestBody接收数组
    @ResponseBody
    @RequestMapping("send/array/two.html")
    public String testAjax02(@RequestBody List<Integer> array){
        for (Integer integer : array) {
            System.out.println(integer);
        }
        return "success";
    }
    //通过一个工具类统一返回的数据
    @ResponseBody
    @RequestMapping("send/compose/object.json")
    public ResultEntity<Student> testResultEntity(@RequestBody Student student){
        return ResultEntity.successWthData(student);
    }

}
